package com.huijutao;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Vibrator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.huijutao.beans.FindItem;
import com.huijutao.beans.GroupItem;
import com.lidroid.xutils.exception.DbException;

import java.io.File;


public class SettingsActivity extends ActionBarActivity {

    private ToggleButton tuisong;
    private ToggleButton shengyin;
    private ToggleButton zhendong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        Toolbar toolbar= (Toolbar) findViewById(R.id.setting_toolbar);
        setSupportActionBar(toolbar);
        tuisong = (ToggleButton) findViewById(R.id.setting_tuisong);
        shengyin = (ToggleButton) findViewById(R.id.setting_shengyin);
        zhendong = (ToggleButton) findViewById(R.id.setting_zhendong);
        Button huancun= (Button) findViewById(R.id.setting_huancun);
        if (tuisong.isChecked()) {
            shengyin.setClickable(true);
            zhendong.setClickable(true);
        }else {
            shengyin.setChecked(false);
            shengyin.setClickable(false);
            zhendong.setChecked(false);
            zhendong.setClickable(false);
        }
        tuisong.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (!isChecked) {
                    shengyin.setChecked(false);
                    shengyin.setClickable(false);
                    zhendong.setChecked(false);
                    zhendong.setClickable(false);
                }else {
                    shengyin.setClickable(true);
                    zhendong.setClickable(true);
                }
            }
        });
        zhendong.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    Vibrate(100);
                }
            }
        });
        huancun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cleanInternalCache(SettingsActivity.this);
                Toast.makeText(SettingsActivity.this,"已清除缓存",Toast.LENGTH_SHORT).show();
                new AlertDialog.Builder(SettingsActivity.this)
                        .setMessage("是否清除收藏列表?点击确定按钮清除收藏列表,点击取消按钮返回")
                        .setTitle("提醒")
                        .setPositiveButton("确定",new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                try {
                                    MyApp.dbUtils.deleteAll(GroupItem.class);
                                    MyApp.dbUtils.deleteAll(FindItem.class);
                                } catch (DbException e) {
                                    e.printStackTrace();
                                }
                                Toast.makeText(SettingsActivity.this,"收藏列表已经清除",Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消",null)
                        .show();
            }
        });
    }
    /**
     * 设置手机震动
     */
    public void Vibrate(long milliseconds) {
        Vibrator vib = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
        vib.vibrate(milliseconds);
    }
    /**
     * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache)
     *
     * @param context
     */
    public static void cleanInternalCache(Context context) {
        deleteFilesByDirectory(context.getCacheDir());
    }
    private static void deleteFilesByDirectory(File directory) {
        if (directory != null && directory.exists() && directory.isDirectory()) {
            for (File item : directory.listFiles()) {
                item.delete();
            }
        }
    }
}
